package com.yvtian.admin.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.yvtian.admin.pojo.SysMenu;
import com.yvtian.admin.service.SysMenuService;
import com.yvtian.constants.TokenConstants;
import com.yvtian.entity.Result;
import com.yvtian.entity.StatusCode;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * (SysMenu)表控制层
 *
 * @author long
 */
@RestController
@RequestMapping("sysMenu")
public class SysMenuController {
    /**
     * 服务对象
     */
    @Autowired
    private SysMenuService sysMenuService;

    @GetMapping("one")
    public Result selectOne(int id) {
        return new Result(true,"查询成功", StatusCode.OK , sysMenuService.getOne(id));
    }

    @GetMapping("list")
    public Result list(SysMenu sysMenu) {
        return new Result(true,"查询成功", StatusCode.OK , sysMenuService.getList(sysMenu));
    }

    @GetMapping("listByRoleId")
    public Result listByRoleId(int roleId) {
        return new Result(true,"查询成功", StatusCode.OK , sysMenuService.listByRoleId(roleId));
    }

    @GetMapping("listByUserId")
    public Result listByUserId(HttpServletRequest request) {
        String userId = (String) request.getAttribute(TokenConstants.JWT_USER_ID);
        return new Result(true,"查询成功", StatusCode.OK , sysMenuService.listByUserId(userId));
    }

    @GetMapping("listTree")
    public Result listTree(SysMenu sysMenu) {
        return new Result(true,"查询成功", StatusCode.OK , sysMenuService.getListTree(sysMenu));
    }

    @PostMapping("insert")
    public Result insert(SysMenu sysMenu) {
        return new Result(true,"插入成功", StatusCode.OK , sysMenuService.saveData(sysMenu));
    }

    @PostMapping("del")
    public Result del(int id) {
        return new Result(true,"删除成功", StatusCode.OK , sysMenuService.delData(id));
    }

    @PostMapping("update")
    public Result update(@RequestBody SysMenu sysMenu) {
        return new Result(true,"更新成功", StatusCode.OK , sysMenuService.updateById(sysMenu));
    }

}
